//
//  MCBaseViewController.m
//  ZXZX
//
//  Created by menhao on 2018/8/24.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCBaseViewController.h"
#import <MJRefresh/MJRefresh.h>

@interface MCBaseViewController ()



@end

@implementation MCBaseViewController

#pragma mark ------ life cycle --------
- (void)viewDidLoad {
    [super viewDidLoad];
    //通知事件 网络错误时 直接展示网络不通页面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkIsReachabilityNotification) name:NetworkIsReachabilityNotification object:nil];
   
}

- (void)networkIsReachabilityNotification{
    [self showExceptionType:UIViewExceptionNoNetwork];
    [BKIndicationView dismiss];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden=NO;
//    self.navigationController.navigationBar.translucent = NO;
}
/*
 *  // 隐藏页面
 */
- (void)hideExceptionView{
    [self.noDataView ly_hideEmptyView];
    [self.noNetworkView ly_hideEmptyView];
    [self.errNetworkView ly_hideEmptyView];
}

/*
 *  // 根据不同类型展示的错误页面
 */
- (void)showExceptionType:(UIViewException)type{

    switch (type) {
            
        case UIViewExceptionNoData:{
            self.tableView.ly_emptyView = self.noDataView;
        }
            break;
        case UIViewExceptionNoNetwork:{
            self.tableView.ly_emptyView = self.noNetworkView;
        }
            break;
        case UIViewExceptionErrNetwork:{
            self.tableView.ly_emptyView = self.errNetworkView;
            
        }
            break;
        default:
            break;
    }

    [self.tableView ly_showEmptyView];
}
/*
 *  // 点击后无网络的重试
 */
- (void)noNetworkAction{
    [self loadListData];
}
/*
 *  // 点击后无服务的重试
 */
- (void)errNetworkAction{
    [self loadListData];
}

- (void)loadListData{
    
}
/*
 *  //根据不同的errcode 类型展示页面 showExcaptionView
 */
- (void)showExcaptionView:(NSDictionary *)errorCode{
    
    if ([errorCode[@"code"] isKindOfClass:[NSError class]]) {
        NSError *err = errorCode[@"code"];
        [self showExcaptionViewWithErr:err];
    }else{
        [self showExceptionType:UIViewExceptionErrNetwork];
    }
    [SVProgressHUD dismiss];
    
}
- (void)showExcaptionViewWithErr:(NSError *)err{
    [self showExcaptionViewWithErrCode:err.code];
}

- (void)showExcaptionViewWithErrCode:(NSInteger)errcode{
    
    if (errcode == -1001) {
       [self showExceptionType:UIViewExceptionErrNetwork];
    } else if (errcode == -1009){
        [self showExceptionType:UIViewExceptionErrNetwork];
    }else {
        [self showExceptionType:UIViewExceptionNoNetwork];
    }
}
/*
 *  //根据data判断是否展示无数据页面
 */

- (void)shouldShowExceptionViewWithManger:(NSDictionary *)manager{
    if (self.tableViewDataArray.count == 0) {
        [self showExceptionType:UIViewExceptionNoData];
    }else{
        [self.tableView reloadData];
        [self hideExceptionView];
        
    }
    if (self.tableViewDataArray.count==[manager[@"DataCount"] integerValue]) {
        self.tableView.mj_footer.hidden=YES;
    }
}

#pragma mark -- getter and setter
/*
 *  lazy property
 */
- (MCEmptyView *)noDataView{
    if (_noDataView == nil) {
        MCEmptyView *view = [MCEmptyView diyEmptyView];
        view.emptyViewIsCompleteCoverSuperView = YES;
        _noDataView = view;
    }
    return _noDataView;
}

- (MCEmptyView *)noNetworkView{
    if (_noNetworkView == nil) {
        MCEmptyView *view = [MCEmptyView diyNoNetworkViewWithTarget:self action:@selector(noNetworkAction)];
        view.emptyViewIsCompleteCoverSuperView = YES;
        _noNetworkView = view;
    }
    return _noNetworkView;
}
- (MCEmptyView *)errNetworkView{
    if (_errNetworkView == nil) {
        MCEmptyView *view = [MCEmptyView diyNoNetworkViewWithTarget:self action:@selector(errNetworkAction)];
        view.emptyViewIsCompleteCoverSuperView = YES;
        _errNetworkView = view;
    }
    return _errNetworkView;
}


/*
 *  日历类相关
 */
- (void)datePickerY:(CGFloat)y{
    self.datePicker.y = y;
    self.datePicker.hidden = YES;
   
}
- (NSDateFormatter *)dateFormat{
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
    [dataFormatter setDateFormat:@"yyyy/MM/dd"];
    return  dataFormatter;
}
- (void)cancelDatePicker{
    WS(weakself);
    self.datePicker.cancelBlock = ^{
        [weakself cancelDatePickerAnimate];
    };
}
- (void)cancelDatePickerAnimate{
    [UIView animateWithDuration:0.25 animations:^{
        [self.datePicker removeFromSuperview];
    }];
}

- (DatePickerView *)datePicker{
    
    if (_datePicker == nil) {
        DatePickerView *datePicker =[[[NSBundle mainBundle] loadNibNamed:@"DatePickerView" owner:self options:0] lastObject];
        datePicker.minDate = self.minDate;
        datePicker.maxDate = self.maxDate;
        datePicker.frame =CGRectMake(0, self.view.frame.size.height  - 200 -kTabBarHeight , self.view.frame.size.width, 200);
        datePicker.Datetitle =@"日期选择";
        [self.view addSubview:datePicker];
        _datePicker = datePicker;
        
    }
    _datePicker.hidden = NO;
    [self.view bringSubviewToFront:_datePicker];
    return _datePicker;
}
/*
 *  tableview 的数据源 如果不用这个字段 需要重写一下shouldShowExceptionViewWithManger方法 或者把这个方法的逻辑下放
 */
- (NSMutableArray *)tableViewDataArray{
    
    if (_tableViewDataArray == nil) {
        _tableViewDataArray = [NSMutableArray array];
    }
    return _tableViewDataArray;
}
@end
